import {
  getStores,
  deleteStores,
  postStores,
  putStores,
  getStoresByNameLike,
  getStoreClass,
  getUsers,
  putFacilitator,
  getStoreCity,
} from '../../../services/api';

export default {
  namespace: 'facilitatorStore',

  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetch({ payload: currentPage }, { call, put }) {
      const response = yield call(getStores, currentPage);
      const category = yield call(getStoreClass);
      const allUsers = yield call(getUsers, 'merchant', currentPage);
      const storeCity = yield call(getStoreCity);
      yield put({
        type: 'save',
        payload: { response, category, allUsers, storeCity },
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(postStores, payload);
      yield put({
        type: 'append',
        payload: response,
      });
      if (callback) callback();
    },
    *remove({ payload, callback }, { call, put }) {
      const { id, currentIndex } = payload;
      yield call(deleteStores, id);
      yield put({
        type: 'delete',
        payload: currentIndex,
      });
      if (callback) callback();
    },
    *audit({ payload, callback }, { call }) {
      const { id, status } = payload;
      yield call(putFacilitator, id, { status });
      if (callback) callback();
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(putStores, payload.id, payload);
      yield put({
        type: 'replace',
        payload: {
          editableIndex: payload.editableIndex,
          response,
        },
      });
      if (callback) callback();
    },
    *search({ payload }, { call, put }) {
      const response = yield call(getStoresByNameLike, payload);
      const category = yield call(getStoreClass);
      const allUsers = yield call(getUsers, 'merchant');
      const storeCity = yield call(getStoreCity);
      yield put({
        type: 'save',
        payload: { response, category, allUsers, storeCity },
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        data: payload.response,
        category: payload.category.list,
        users: payload.allUsers.list,
        storeCity: payload.storeCity.list,
      };
    },
    append(state, action) {
      const { list } = state.data;
      list.unshift(action.payload);
      const total = state.data.pagination.total + 1;
      return {
        ...state,
        data: {
          list,
          pagination: {
            ...state.data.pagination,
            total,
          },
        },
      };
    },
    delete(state, action) {
      const { list } = state.data;
      list.splice(action.payload, 1);
      const total = state.data.pagination.total - 1;
      return {
        ...state,
        data: {
          list,
          pagination: {
            ...state.data.pagination,
            total,
          },
        },
      };
    },
    replace(state, action) {
      const { list } = state.data;
      list.splice(action.payload.editableIndex, 1, action.payload.response);
      return {
        ...state,
        data: {
          list,
          pagination: {
            ...state.data.pagination,
          },
        },
      };
    },
  },
};
